/*
 * @Description: 动态表单模板数据模型
 */
import { ApiProperty } from '@nestjs/swagger';
import { modelOptions, prop } from '@typegoose/typegoose';
import { IsNotEmpty, IsOptional, IsEnum, IsBoolean } from 'class-validator';

// 字段类型枚举
export enum FieldType {
  TEXT = 'text',
  TEXTAREA = 'textarea',
  NUMBER = 'number',
  EMAIL = 'email',
  PHONE = 'phone',
  DATE = 'date',
  SELECT = 'select',
  RADIO = 'radio',
  CHECKBOX = 'checkbox',
  FILE = 'file'
}

// 表单字段定义
export class FormField {
  @ApiProperty({ description: '字段名称' })
  @IsNotEmpty({ message: '字段名称不能为空' })
  @prop({ required: true })
  fieldName: string;

  @ApiProperty({ description: '字段标签' })
  @IsNotEmpty({ message: '字段标签不能为空' })
  @prop({ required: true })
  fieldLabel: string;

  @ApiProperty({ description: '字段描述' })
  @IsOptional()
  @prop()
  fieldDescription?: string;

  @ApiProperty({ description: '字段类型', enum: FieldType })
  @IsEnum(FieldType, { message: '字段类型不正确' })
  @prop({ required: true, enum: FieldType })
  fieldType: FieldType;

  @ApiProperty({ description: '是否必填' })
  @IsBoolean({ message: '是否必填必须为布尔值' })
  @prop({ required: true, default: false })
  required: boolean;

  @ApiProperty({ description: '字段选项（用于select、radio、checkbox类型）' })
  @IsOptional()
  @prop({ type: [String] })
  options?: string[];

  @ApiProperty({ description: '默认值' })
  @IsOptional()
  @prop()
  defaultValue?: string;

  @ApiProperty({ description: '字段排序' })
  @IsOptional()
  @prop({ default: 0 })
  sortOrder?: number;

  @ApiProperty({ description: '验证规则（正则表达式）' })
  @IsOptional()
  @prop()
  validationRule?: string;

  @ApiProperty({ description: '验证错误提示' })
  @IsOptional()
  @prop()
  validationMessage?: string;
}

@modelOptions({
  schemaOptions: {
    timestamps: true,
  },
})
export class FormTemplate {
  @ApiProperty({ description: '表单名称' })
  @IsNotEmpty({ message: '表单名称不能为空' })
  @prop({ required: true })
  formName: string;

  @ApiProperty({ description: '表单描述' })
  @IsOptional()
  @prop()
  formDescription?: string;

  @ApiProperty({ description: '表单字段列表' })
  @prop({ type: [FormField], default: [] })
  fields: FormField[];

  @ApiProperty({ description: '表单状态（启用/禁用）' })
  @IsBoolean({ message: '表单状态必须为布尔值' })
  @prop({ default: true })
  isActive: boolean;

  @ApiProperty({ description: '创建者ID' })
  @IsOptional()
  @prop()
  createdBy?: string;

  @ApiProperty({ description: '表单唯一标识符' })
  @IsOptional()
  @prop({ unique: true })
  formKey?: string;

  _id: string;
  createdAt: Date;
  updatedAt: Date;
}